const router = require('koa-router')()
const userService = require('../controllers/mySqlControll.js')
const jwt = require('../untils/jwt.js')
const { getDetailedDateTime } = require('../untils/timeUtil.js')

// 获取轮播图数据
router.get('/gettingRotating', async (ctx, next) => {
  try {
    const result = await userService.gettingRotating()
    if (result) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 获取轮播图数据注释名字
router.get('/gettingRotatingAnnotations', async (ctx, next) => {
  try {
    const result = await userService.gettingRotatingAnnotations()
    if (result) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 添加轮播图
router.post('/addRotating', async (ctx, next) => {
  try {
    const { image_url, target_url, created_at } = ctx.request.body; vb
    // 检查必填字段是否为空
    if (!image_url || !target_url) {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '输入框不能为空'
      };
    } else {
      await userService.addRotating([image_url, target_url, created_at]).then(res => {
        // 检查添加是否成功
        if (res.affectedRows !== 0) {
          ctx.body = {
            msgType: '8000',
            code: 0,
            data: '',
            msg: '添加成功'
          };
        } else {
          // 添加失败的情况
          ctx.body = {
            msgType: '8002',
            code: 0,
            data: '',
            msg: '添加失败'
          };
        }
      })
    }
  } catch (error) {
    // 错误处理
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    };
  }
});

// 修改轮播图
router.post('/changeRotating', async (ctx, next) => {
  const { image_url, target_url, updated_at, rotate_id } = ctx.request.body
  try {
    if (!image_url || !target_url) {
      ctx.body = {
        msgType: '8002',
        code: 0,
        msg: '输入框不能为空'
      }
    }
    await userService.changeRotating(image_url, target_url, updated_at, rotate_id).then(res => {
      if (res.affectedRows !== 0) {
        ctx.body = {
          msgType: '8000',
          code: 0,
          msg: '修改成功'
        }
      } else {
        ctx.body = {
          msgType: '8002',
          code: 0,
          msg: '修改失败'
        }
      }
    }
    )
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 删除轮播图
router.post('/deleteRotating', async (ctx, next) => {
  const { rotate_id } = ctx.request.body
  try {
    const results = await Promise.all(rotate_id.map(async (id) => {
      return await userService.deleteRotating(id);
    }));
    const affectedRows = results.reduce((total, result) => total + result.affectedRows, 0); // 获取所有删除操作受影响的行数总和
    if (affectedRows !== 0) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: '',
        msg: '删除成功'
      }
    } else {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '删除失败'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 获取活动的数据表
router.get('/getEvent', async (ctx, next) => {
  try {
    const result = await userService.getEvent()
    if (result) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 获取活动的数据表名
router.get('/getEventAnnotations', async (ctx, next) => {
  try {
    const result = await userService.getEventAnnotations()
    if (result) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 增加活动的数据
router.post('/addEvent', async (ctx, next) => {
  try {
    const { event_name, description, image_url, start_date, end_date, created_at } = ctx.request.body;
    // 检查必填字段是否为空
    if (!event_name || !image_url || !description || start_date === '' || end_date === '') {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '输入框不能为空'
      }
    } else {
      await userService.addEvent([event_name, description, image_url, start_date, end_date, created_at]).then(res => {
        // 检查添加是否成功
        if (res.affectedRows !== 0) {
          ctx.body = {
            msgType: '8000',
            code: 0,
            data: '',
            msg: '添加成功'
          };
        } else {
          // 添加失败的情况
          ctx.body = {
            msgType: '8002',
            code: 0,
            data: '',
            msg: '添加失败'
          };
        }
      })
    }
  } catch (error) {
    // 错误处理
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    };
  }
});

// 修改活动的数据
router.post('/changeEvent', async (ctx, next) => {
  const { event_name, description, image_url, start_date, end_date, updated_at, event_id } = ctx.request.body
  try {
    if (!event_name || !image_url || !description || start_date === '' || end_date === '') {
      ctx.body = {
        msgType: '8002',
        code: 0,
        msg: '输入框不能为空'
      }
    } else {
      await userService.changeEvent(event_name, description, image_url, start_date, end_date, updated_at, event_id).then(res => {
        if (res.affectedRows !== 0) {
          ctx.body = {
            msgType: '8000',
            code: 0,
            msg: '修改成功'
          }
        } else {
          ctx.body = {
            msgType: '8002',
            code: 0,
            msg: '修改失败'
          }
        }
      }
      )
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 删除活动数据
router.post('/deleteEvent', async (ctx, next) => {
  const { event_id } = ctx.request.body
  try {
    const results = await Promise.all(event_id.map(async (id) => {
      return await userService.deleteEvent(id);
    }));
    const affectedRows = results.reduce((total, result) => total + result.affectedRows, 0); // 获取所有删除操作受影响的行数总和
    if (affectedRows !== 0) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: '',
        msg: '删除成功'
      }
    } else {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '删除失败'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})


// 获取公告的数据表
router.get('/getBulletin', async (ctx, next) => {
  try {
    const result = await userService.getBulletin()
    if (result) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 获取公告的数据表名
router.get('/getBulletinAnnotations', async (ctx, next) => {
  try {
    const result = await userService.getBulletinAnnotations()
    if (result) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 增加公告的数据
router.post('/addBulletin', async (ctx, next) => {
  try {
    const {bulletin_name,bulletin_content,created_at } = ctx.request.body;
    // 检查必填字段是否为空
    if (!bulletin_name || !bulletin_content ) {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '输入框不能为空'
      }
    } else {
      await userService.addBulletin([bulletin_name,bulletin_content,created_at]).then(res => {
        // 检查添加是否成功
        if (res.affectedRows !== 0) {
          ctx.body = {
            msgType: '8000',
            code: 0,
            data: '',
            msg: '添加成功'
          };
        } else {
          // 添加失败的情况
          ctx.body = {
            msgType: '8002',
            code: 0,
            data: '',
            msg: '添加失败'
          };
        }
      })
    }
  } catch (error) {
    // 错误处理
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    };
  }
});

// 删除活动数据
router.post('/deleteBulletin', async (ctx, next) => {
  const { bulletin_id } = ctx.request.body
  try {
    const results = await Promise.all(bulletin_id.map(async (id) => {
      return await userService.deleteBulletin(id);
    }));
    const affectedRows = results.reduce((total, result) => total + result.affectedRows, 0); // 获取所有删除操作受影响的行数总和
    if (affectedRows !== 0) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: '',
        msg: '删除成功'
      }
    } else {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '删除失败'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})



module.exports = router